package com.youmu.sichuansfc.dao.impl;

import com.youmu.sichuansfc.dao.FundUserDao;
import com.youmu.sichuansfc.domain.YoumuFundUser;
import com.youmu.sichuansfc.domain.pojo.FundUserPojo;
import com.youmu.sichuansfc.dto.PageT;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class FundUserDaoImpl implements FundUserDao {

    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public YoumuFundUser findByToken(String token) {

        String sql = "from YoumuFundUser where token =:token and is_delete = 0";
        List<YoumuFundUser> youmuFundUsers = em.createQuery(sql, YoumuFundUser.class)
                .setParameter("token", token)
                .getResultList();
        if(youmuFundUsers != null && youmuFundUsers.size() > 0){
            return youmuFundUsers.get(0);
        }
        return null;
    }

    @Override
    public YoumuFundUser findByPhone(String phone) {
        String sql = "from YoumuFundUser where phone =:phone";
        List<YoumuFundUser> youmuFundUsers = em.createQuery(sql, YoumuFundUser.class)
                .setParameter("phone", phone)
                .getResultList();
        if(youmuFundUsers != null && youmuFundUsers.size() > 0){
            return youmuFundUsers.get(0);
        }
        return null;
    }

    @Override
    public YoumuFundUser update(YoumuFundUser youmuFundUser) {
        return em.merge(youmuFundUser);
    }

    @Override
    public PageT findFundUsers(Integer fundUserStatus, String fundManagerName, int num, int size) {

        Map<String, Object> map = new HashMap<>();
        StringBuffer sql = new StringBuffer();
        sql.append(" select yfu.`password`, yfu.phone, yfu.add_time, yfu.is_delete, yfu.id, \n");
        sql.append(" case yfm.contact_name when null then '' else yfm.contact_name end as contact_name, \n");
        sql.append(" case yfm.contact_email when null then '' else yfm.contact_email  end as contact_email , \n");
        sql.append(" case yfm.manager_name when null then '' else yfm.manager_name end as manager_name\n");
        sql.append(" from youmu_fund_user yfu \n");
        sql.append(" left join youmu_fund_manager yfm \n");
        sql.append(" on yfu.id = yfm.fund_user_id \n");
        sql.append(" where 1 = 1 \n");

        if (fundUserStatus != null){
            sql.append("and  yfu.is_delete =:isDelete \n");
            map.put("isDelete", 1 == fundUserStatus.intValue() ? true :false);
        }

        if (StringUtils.isNotEmpty(fundManagerName)){
            sql.append(" and yfm.manager_name like :fundManagerName \n");
            map.put("fundManagerName", "%"+fundManagerName+"%");
        }
        sql.append(" group by  yfu.id order by yfu.add_time desc  \n");

        Query query = em.createNativeQuery(sql.toString(), FundUserPojo.class);

        for (String key : map.keySet()){
            query.setParameter(key, map.get(key));
        }

        PageT pageT = new PageT();
        pageT.setCount(query.getResultList().size());
        pageT.setPageList(query.setFirstResult(num*size)
                            .setMaxResults(size)
                            .getResultList());
        return pageT;
    }

    @Override
    public YoumuFundUser findById(Long id) {
        String sql = "from YoumuFundUser where id=:id";
        List<YoumuFundUser> youmuFundUsers = em.createQuery(sql, YoumuFundUser.class)
                .setParameter("id", id)
                .getResultList();
        if(youmuFundUsers != null && youmuFundUsers.size() > 0){
            return youmuFundUsers.get(0);
        }
        return null;
    }

    @Override
    public List<YoumuFundUser> findByTime(Calendar startTime, Calendar endTime) {
        String sql = "from YoumuFundUser where addTime >=:startTime and addTime <=:endTime ";
        List<YoumuFundUser> youmuFundUserLoginHistoryList = em.createQuery(sql, YoumuFundUser.class)
                .setParameter("startTime", startTime.getTime())
                .setParameter("endTime", endTime.getTime())
                .getResultList();
        return youmuFundUserLoginHistoryList;
    }
}
